package com.character.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.character.entity.Dept;
import com.character.entity.MyReturn;
import com.character.entity.Question;
import com.character.entity.User;
import com.character.service.DeptService;
import com.character.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Dept)表控制层
 *
 * @author makejava
 * @since 2021-12-02 16:17:02
 */
@RestController
@RequestMapping("dept")
public class DeptController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;
    @Resource
    private UserService userService;

    @RequestMapping("selectAll")
    public MyReturn selectAll(Integer limit, Integer page, Dept dept) {
        Page<Dept> deptPage = new Page<>(page,limit);
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dept.getDeptName())){
            queryWrapper.like("dept_name",dept.getDeptName());
        }
        if(ObjectUtil.isNotEmpty(dept.getDeptLoc())){
            queryWrapper.like("dept_loc",dept.getDeptLoc());
        }

        Page<Dept> page1 = this.deptService.page(deptPage,queryWrapper);
        return super.success(page1.getRecords(),page1.getTotal());
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.deptService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dept 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public MyReturn insert(@RequestBody Dept dept) {
        boolean save = this.deptService.save(dept);
        if (save) {
            return super.success(dept,1L);
        } else {
            return super.failed(dept,1L);
        }
    }

    /**
     * 修改数据
     *
     * @param dept 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public MyReturn update(@RequestBody Dept dept) {
        boolean update = this.deptService.updateById(dept);
        if (update) {
            return super.success(dept,1L);
        } else {
            return super.failed(dept,1L);
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public MyReturn delete(@RequestParam("idList[]") List<Long> idList) {
        List<User> userList = userService.list();
        for (User user : userList) {
            for (Long deptId : idList) {
                if(user.getDeptId().toString().equals(deptId.toString())){
                    return super.failed("部门中有用户,无法删除");
                }
            }
        }
        boolean delete = this.deptService.removeByIds(idList);
        if (delete) {
            return super.success(null);
        } else {
            return super.failed(null);
        }
    }
    @RequestMapping("/getCount")
    public MyReturn getCount(){
        int count = this.deptService.count();
        return success(null, (long) count);
    }
    @RequestMapping("/getAllDeptNoPage")
    public MyReturn getAllDeptNoPage(){
        List<Dept> list = deptService.list();
        return super.success(list, (long) list.size());
    }
}

